<?php
/**
 * 上传附件和上传视频
 * User: Jinqn
 * Date: 14-04-09
 * Time: 上午10:17
 */
include "Uploader.class.php";

function image($file,$folder = 'home')
{
    $allowedExts = array("jpeg", "jpg", "png");
    $temp = explode(".", $file["name"]);
    $extension = end($temp);        // 获取文件后缀名
    if ((($file["type"] == "image/gif")
            || ($file["type"] == "image/jpeg")
            || ($file["type"] == "image/jpg")
            || ($file["type"] == "image/pjpeg")
            || ($file["type"] == "image/x-png")
            || ($file["type"] == "image/png"))
        && ($file["size"] < 5242880)    // 小于 5M
        && in_array($extension, $allowedExts)) {
        if ($file["error"] > 0) {
            return;
        } else {
            $url = '../../../img/'.$folder.'/';
            if (!is_dir($url)) {
                mkdir($url, 0777, true);
            }
            $imgName = time() . rand(10000, 99999) . '.jpg';
            $urlNaem = move_uploaded_file($file["tmp_name"], $url.$imgName);
            if ($urlNaem) {
                return [
                    'original'=>$file["name"],
                    'state'=> "SUCCESS",
                    'url'=>'http://'.$_SERVER['HTTP_HOST']."/img/".$folder."/".$imgName
                ];
            } else {
                return;
            }
        }
    } else {
        return;
    }
}


/* 上传配置 */
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
    case 'uploadimage':
        return json_encode(image($_FILES['upfile'],'admin'));
        $config = array(
            "pathFormat" => $CONFIG['imagePathFormat'],
            "maxSize" => $CONFIG['imageMaxSize'],
            "allowFiles" => $CONFIG['imageAllowFiles']
        );
        $fieldName = $CONFIG['imageFieldName'];
        break;
    case 'uploadscrawl':
        $config = array(
            "pathFormat" => $CONFIG['scrawlPathFormat'],
            "maxSize" => $CONFIG['scrawlMaxSize'],
            "allowFiles" => $CONFIG['scrawlAllowFiles'],
            "oriName" => "scrawl.png"
        );
        $fieldName = $CONFIG['scrawlFieldName'];
        $base64 = "base64";
        break;
    case 'uploadvideo':
        $config = array(
            "pathFormat" => $CONFIG['videoPathFormat'],
            "maxSize" => $CONFIG['videoMaxSize'],
            "allowFiles" => $CONFIG['videoAllowFiles']
        );
        $fieldName = $CONFIG['videoFieldName'];
        break;
    case 'uploadfile':
    default:
        $config = array(
            "pathFormat" => $CONFIG['filePathFormat'],
            "maxSize" => $CONFIG['fileMaxSize'],
            "allowFiles" => $CONFIG['fileAllowFiles']
        );
        $fieldName = $CONFIG['fileFieldName'];
        break;
}

/* 生成上传实例对象并完成上传 */
$up = new Uploader($fieldName, $config, $base64);

/**
 * 得到上传文件所对应的各个参数,数组结构
 * array(
 *     "state" => "",          //上传状态，上传成功时必须返回"SUCCESS"
 *     "url" => "",            //返回的地址
 *     "title" => "",          //新文件名
 *     "original" => "",       //原始文件名
 *     "type" => ""            //文件类型
 *     "size" => "",           //文件大小
 * )
 */

/* 返回数据 */
$fileInfo = $up->getFileInfo();
$file = array(
    "name" => $fileInfo["title"],
    "tmp_name" => $_SERVER['DOCUMENT_ROOT'].$fileInfo["url"]
);
$uploadQiniu = $up->uploadImg( $file );
if ( $uploadQiniu["code"] == 1 ) {
    $fileUrl = $up->getDownloadUrl( $uploadQiniu["data"] );
}
if ( unlink($_SERVER['DOCUMENT_ROOT'].$fileInfo["url"]) ) {
    $fileInfo["unlink"] = 1;
}
$fileInfo["url"] = $fileUrl;
return json_encode( $fileInfo );
